package leetcode_601_700;

public class LeeCode_696 {
    public static void main(String[] args) {

    }
    private static int countBinarySubstrings(String S) {
        char[] s = S.toCharArray();
        int ans = 0;
        int index = 0, n = s.length;
        while (index < n - 1){
            if (s[index] == '0'){
                if (s[index + 1] == '1'){
                    int c = 0, l = index, r = index + 1;
                    while (l >= 0 && r < n && s[l] == '0' && s[r] == '1'){
                        l--;
                        r++;
                        c++;
                    }
                    ans += c;
                }
            }else if (s[index] == '1'){
                if (s[index + 1] == '0'){
                    int c = 0, l = index, r = index + 1;
                    while (l >= 0 && r < n && s[l] == '1' && s[r] == '0'){
                        l--;
                        r++;
                        c++;
                    }
                    ans += c;
                }
            }
            index++;
        }
        return ans;
    }
}
